package com.youyicloud.service;

import com.youyicloud.entity.EquipmentInfo;
import com.youyicloud.entity.EquipmentPay;
import com.youyicloud.entity.PayInfo;
import com.youyicloud.repository.EquipmentPayRepository;
import com.youyicloud.repository.EquipmentRepository;
import com.youyicloud.repository.PayRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EquipmentPayService {

    @Autowired
    private EquipmentPayRepository equipmentPayRepository;
    @Autowired
    private EquipmentRepository equipmentRepository;
    @Autowired
    private PayRepository payRepository;

    public void deleteByEquipmentPayId(Integer equipmentPayId){
        equipmentPayRepository.delete(equipmentPayId);
    }

    public EquipmentPay findByEquipmentPayId(Integer equipmentPayId){
        EquipmentPay equipmentPay = equipmentPayRepository.findOne(equipmentPayId);
        return equipmentPay;
    }

    public EquipmentPay findByEquipmentId(Integer equipmentId){
        EquipmentPay equipmentPay = equipmentPayRepository.findByEquipmentId(equipmentId);
        return equipmentPay;
    }

    public EquipmentPay saveEquipmentPay(String imeiMac,String equipmentBrevityCode,String payUrl,Integer equipmentTypeId){
        EquipmentInfo equipmentInfo = new EquipmentInfo();
        equipmentInfo.setImeiMac(imeiMac);
        equipmentInfo.setEquipmentTypeId(equipmentTypeId);
        equipmentRepository.save(equipmentInfo);
        PayInfo payInfo = new PayInfo();
        payInfo.setPayUrl(payUrl);
        payInfo.setEquipmentBrevityCode(equipmentBrevityCode);
        payRepository.save(payInfo);
        EquipmentPay equipmentPay = new EquipmentPay();
        equipmentPay.setEquipmentId(equipmentInfo.getEquipmentId());
        equipmentPay.setPayId(payInfo.getPayId());
        equipmentPayRepository.save(equipmentPay);
        return equipmentPay;
    }


}
